<!DOCTYPE html>
<!--
Copyright 2010 WebDriver committers
Copyright 2010 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
  <title>attribute_test.html</title>
  <script src="test_bootstrap.js"></script>
  <script type="text/javascript">
    goog.require('bot.dom');
    goog.require('bot.locators');
    goog.require('goog.events.EventType');
    goog.require('goog.testing.jsunit');
  </script>

  <script type="text/javascript">
    function testCanFindNamedAttributes() {
      var e = bot.locators.findElement({id: 'cheddar'});

      assertEquals('cheese1', bot.dom.getAttribute(e, 'name'));
    }

    function testCanFindAttributesOnTheExpando() {
      var e = bot.locators.findElement({id: 'cheddar'});

      assertEquals('lovely', bot.dom.getAttribute(e, 'unknown'));
    }

    function testReturnsNullWhenSelectedAttributeCannotBeSet() {
      var e = bot.locators.findElement({id: 'cheddar'});

      assertEquals(null, bot.dom.getAttribute(e, 'selected'));
    }

    function testShouldReturnClassAttribute() {
      var e = bot.locators.findElement({id: 'brie'});

      assertEquals('tasty', bot.dom.getAttribute(e, 'class'));
    }

    function testShouldReturnNullForMissingAttributes() {
      var e = bot.locators.findElement({id: 'gouda'});

      assertNull(bot.dom.getAttribute(e, 'never_there'));
      assertNull(bot.dom.getAttribute(e, 'class'));
    }

    function testShouldReturnAnEmptyStringWhenAttributesValueIsAnEmptyString() {
      var e = bot.locators.findElement({id: 'gouda'});

      assertEquals('', bot.dom.getAttribute(e, 'empty'));
    }

    function testReturnsTrueWhenBooleanAttributesPresent() {
      var e1 = bot.locators.findElement({id: 'peas'});
      var e2 = bot.locators.findElement({name: 'checky'});
      var e3 = bot.locators.findElement({id: 'checked'});

      assertReturnsTrue(e1, 'selected');
      assertReturnsTrue(e2, 'checked');
      assertReturnsTrue(e2, 'readonly');
      assertReturnsTrue(e3, 'checked');

      function assertReturnsTrue(element, attribute) {
        assertEquals('Should return "true" for attribute "' + attribute + '"',
            'true', bot.dom.getAttribute(element, attribute));
      }
    }

    function testAttributeMatchesAreCaseInsensitive() {
      var e = bot.locators.findElement({name: 'checky'});

      assertEquals(bot.dom.getAttribute(e, 'readonly'),
                   bot.dom.getAttribute(e, 'readOnly'));
      assertEquals(bot.dom.getAttribute(e, 'name'),
                   bot.dom.getAttribute(e, 'NaMe'));
    }

  </script>
</head>
<body>
  <div name="cheese1" id="cheddar" unknown="lovely">Cheddar</div>
  <div name="cheese2" id="brie" class="tasty">Brie</div>
  <div name="cheese3" id="gouda" empty="">Gouda</div>

  <form action="#" method="get" name="myform">
    <select name="fish_supper">
      <option value="fish">Fish</option>
      <option id="chips" value="chips">Chips</option>
      <option id="peas"  value="peas" selected>Mushy Peas</option>
      <option value="gravy">Gravy</option>
    </select>

    <!-- Setting checked="false" but getAttribute should return true. -->
    <input name="checky" type="checkbox" checked="false" readonly="readonly"/>
    <input name="radio" id="unchecked" type="radio" value="me">Me
    <input name="radio" id="checked" type="radio" value="myself"
           checked="checked">Myself
    <input name="radio" type="radio" value="i">And I
  </form>
</body>
</html>
